﻿// using System.Text;
// using Admin.ApplicationLib.Jobs;
// using Admin.ApplicationLib.Repositories;
// using Admin.ApplicationLib.Specifications;
// using Admin.CommonLib.Constants;
// using Admin.CommonLib.Models.Job;
// using AutoMapper;
// using Newtonsoft.Json;
// using Quartz;
// using Swimj.CommonLib.Base;
//
// namespace Admin.ApplicationLib.Services
// {
//     public class JobService : IJobService
//     {
//         private readonly IJobDetailRepository _jobDetailRepository;
//         private readonly ISchedulerFactory _schedulerFactory;
//         private readonly IMapper _mapper;
//
//         public JobService(IJobDetailRepository jobDetailRepository,
//             ISchedulerFactory schedulerFactory,
//             IMapper mapper)
//         {
//             _jobDetailRepository = jobDetailRepository;
//             _schedulerFactory = schedulerFactory;
//             _mapper = mapper;
//         }
//
//         public async Task<PaginatedResult<JobDetailInfoResponseDto>> QueryJobsByPageAsync(
//             JobDetailSearchRequestDto model)
//         {
//             var jobSpecification = new JobDetailSpecification(model);
//
//             var jobEntities = await _jobDetailRepository.ListAsync(jobSpecification);
//
//             List<JobDetailInfoResponseDto> list = new List<JobDetailInfoResponseDto>();
//
//             jobEntities.ForEach(i =>
//             {
//                 var jobDetailInfoResponseDto = _mapper.Map<JobDetailInfoResponseDto>(i);
//                 if (i.JobData != null)
//                 {
//                     var s = Encoding.UTF8.GetString(i.JobData);
//                     if (!string.IsNullOrEmpty(s))
//                     {
//                         var jobDataMap = JsonConvert.DeserializeObject<JobDataMap>(s);
//                         jobDetailInfoResponseDto.ProcessPath = jobDataMap?["ProcessPath"].ToString();
//                     }
//                 }
//
//                 list.Add(jobDetailInfoResponseDto);
//             });
//
//             var count = await _jobDetailRepository.CountAsync(jobSpecification);
//             return await PaginatedResult<JobDetailInfoResponseDto>.SuccessAsync(
//                 list,
//                 count,
//                 model.Page, model.Limit);
//         }
//
//         public async Task<Result<IList<JobDetailInfoResponseDto>>> QueryJobsAsync(JobDetailSearchRequestDto model)
//         {
//             var jobSpecification = new JobDetailSpecification(model);
//             var jobEntities = await _jobDetailRepository.ListAsync(jobSpecification);
//
//             return await Result<IList<JobDetailInfoResponseDto>>.SuccessAsync(
//                 _mapper.Map<IList<JobDetailInfoResponseDto>>(jobEntities));
//         }
//
//         public async Task<Result<JobDetailInfoResponseDto>> GetJobAsync(string jobName, string jobGroup)
//         {
//             var jobKey = new JobKey(jobName, jobGroup);
//             var scheduler = await _schedulerFactory.GetScheduler();
//             var jobDetail = await scheduler.GetJobDetail(jobKey);
//             return await Result<JobDetailInfoResponseDto>.SuccessAsync(
//                 _mapper.Map<JobDetailInfoResponseDto>(jobDetail));
//         }
//
//         public async Task<Result<JobDetailInfoResponseDto>> CreateJobAsync(JobDetailEditRequestDto model)
//         {
//             var scheduler = await _schedulerFactory.GetScheduler();
//
//             Type jobType = typeof(LocalLoadJob);
//
//             if (model.JobType == (int)JobTypeEnum.Remote)
//             {
//                 jobType = typeof(RemoteLoadJob);
//             }
//             else if (model.JobType == (int)JobTypeEnum.DataBase)
//             {
//                 jobType = typeof(DataBaseLoadJob);
//             }
//
//             var jobKey = new JobKey(model.JobName, model.JobGroup);
//
//             var dataMap = new JobDataMap
//             {
//                 { "ProcessPath", model.ProcessPath ?? string.Empty }
//             };
//
//             //define the job and tie it to our HelloJob class
//             var job = JobBuilder.Create(jobType)
//                 .WithIdentity(jobKey)
//                 .StoreDurably(true)
//                 .WithDescription(model.Description)
//                 .SetJobData(dataMap)
//                 .Build();
//
//             await scheduler.AddJob(job, false);
//
//             return await Result<JobDetailInfoResponseDto>.SuccessAsync(_mapper.Map<JobDetailInfoResponseDto>(job));
//         }
//
//         public async Task<Result<JobDetailInfoResponseDto>> UpdateJobAsync(Guid id, JobDetailEditRequestDto model)
//         {
//             var jobEntity = await _jobDetailRepository.GetByIdAsync(id);
//             if (jobEntity != null)
//             {
//                 _mapper.Map(model, jobEntity);
//
//                 await _jobDetailRepository.UpdateAsync(jobEntity);
//             }
//
//             return await Result<JobDetailInfoResponseDto>.SuccessAsync(
//                 _mapper.Map<JobDetailInfoResponseDto>(jobEntity));
//         }
//
//         public async Task StartJobAsync(string jobName, string groupName)
//         {
//             var jobKey = new JobKey(jobName, groupName);
//
//             var scheduler = await _schedulerFactory.GetScheduler();
//
//             // var jobDetail = await scheduler.GetJobDetail(jobKey);
//
//             await scheduler.TriggerJob(jobKey);
//         }
//
//         public async Task StopJobAsync(Guid id)
//         {
//             var scheduler = await _schedulerFactory.GetScheduler();
//
//             await scheduler.PauseJob(new JobKey("myJob", "group1"));
//         }
//     }
// }